package com.wyl.selenium.exception;

import com.wyl.selenium.common.ResponseData;
import com.wyl.selenium.enums.ResponseCode;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author WuYiLong
 * @Date 2024/2/4 14:45
 */
@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler(value = ConstraintViolationException.class)
    public ResponseData constraintViolationException(ConstraintViolationException exception) {
        Set<ConstraintViolation<?>> constraintViolations = exception.getConstraintViolations();
        String message = constraintViolations.stream().map(m -> m.getMessage()).collect(Collectors.joining(";"));
        return new ResponseData(ResponseCode.FAIL.getCode(),message);
    }
}
